<template>
  <div>
    <a-spin :spinning="confirmLoading">
      <z-form-container :disabled="disableSubmit">
        <a-form-model slot="detail" layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
          <a-form-model-item label="字典编码" prop="dictCode">
            <a-input placeholder="请输入字典编码" :disabled="disableSubmit || !!form.id" v-model.trim="form.dictCode"/>
          </a-form-model-item>
          <a-form-model-item label="字典名称" prop="dictName">
            <a-input placeholder="请输入字典名称" :disabled="disableSubmit" v-model.trim="form.dictName"/>
          </a-form-model-item>
          <a-form-model-item label="字典类别" prop="dictType">
            <a-select :disabled="disableSubmit" v-model="form.dictType" placeholder="请选择字典类别">
              <a-select-option value="1">系统字典</a-select-option>
              <a-select-option value="2">业务字典</a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="默认值">
            <a-input :disabled="disableSubmit" placeholder="请输入默认值" v-model.trim="form.dictDefaultValue"/>
          </a-form-model-item>
          <a-form-model-item  label="描述">
            <a-input :disabled="disableSubmit" type="textarea" v-model="form.dictDesc"/>
          </a-form-model-item>
        </a-form-model>
      </z-form-container>
    </a-spin>
  </div>
</template>

<script>
import ZFormContainer from '@/components/zz/ZFormContainer'
import ModalFormMixin from '@/components/k-form-design/mixins/ModalFormMixin'
import { getAction, postAction, putAction } from '@/services/manage'
import { CURY_TYPE } from '@/store/mutation-types'
export default {
  name: 'UserModal',
  mixins: [ModalFormMixin],
  components: {
    ZFormContainer
  },
  data() {
    return {
      validatorRules: {},
      url: {
        linelist: '', // 明细数据请求接口
        order: '/sys/dict/tree/', // 主单请求接口
        save: ''
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      }
    }
  },
  methods: {
    classifyIntoFormData() {
      this.form.crudType = this.editType === 'add' ? CURY_TYPE.SAVE : CURY_TYPE.UPDATE
      return {
        ...this.form
      }
    },
    async getData() { // 获取表单数据
      try {
        await this.getAllValidate()
        const formData = this.classifyIntoFormData()
        /**
         * 1. 保存数据
         * 2. 表单为审批节点单据时，需要单独存储节点数据，调用该方法
         * 3. 若数据不存储到业务单据时，可不调用
        */
        await this.save(formData)
        return formData
      } catch (error) {
        console.error(error)
        throw new Error(error)
      }
    },
    save(data) { // 保存表单数据
      return postAction(this.url.save, data)
    },
    setData(data) { // 需要根据businessKey请求业务数据
      // 请求主表数据
      getAction(this.url.order + data.businessKey).then(res => {
        if (res.success) {
          this.form = res.result || {}
        } else {
          this.$message.warning(res.message)
        }
      })
      // 请求明细表数据，请自行实现
      // getAction(this.url.list + data.businessKey).then(res => {
      //   if (res.success) {
      //     this.detail.dataSource = res.result || []
      //   } else {
      //     this.$message.warning(res.message)
      //   }
      // })
    },
    async updateBpmnStatus({ formData, state }) { // 业务单更新流程审批状态使用
      try {
        const row = await getAction(this.url.getOrderById + formData.businessKey) // 获取待审批行数据
        row.bpmnStatus = state // 更新行数据状态
        row.crudType = CURY_TYPE.UPDATE // 更新行数据脏数据状态
        const res2 = await putAction(this.url.save, row) // 保存更新后的行数据
        return res2
      } catch (error) {
        return error
      }
    }
  }
}
</script>
